<?php
/**
 * Body of FloatEdit extension
 *
 * Contains definition of class FloatEdit
 *
 * @author Stephan Gambke
 */


if ( !defined( 'FE_VERSION' ) ) {
	die( 'This file is part of a MediaWiki extension, it is not a valid entry point.' );
}


/**
 * Definition of class FloatEdit
 */
class FloatEdit {

	static function renderFloatLink ( &$parser ) {

		wfDebug( "renderFloatLink\n" );

		// have to reassign parser as a reference
		$params = func_get_args();
		$params[0] = &$parser;

		// ensure loading of jQuery and style sheets
		self::loadScripts( $parser );

		return "<div class='floatlink'>" . call_user_func_array( "SFParserFunctions::renderFormLink", $params ) . "</div>";
	}

	static function renderFloatInput ( &$parser ) {

		wfDebug( "renderFloatInput\n" );

		// have to reassign parser as a reference
		$params = func_get_args();
		$params[0] = &$parser;

		// ensure loading of jQuery and style sheets
		self::loadScripts( $parser );

		return "<div class='floatinput'>" . call_user_func_array( "SFParserFunctions::renderFormInput", $params ) . "</div>";
	}

	static function loadScripts ( &$parser ) {

		global $fefScriptPath;

		if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {

			wfDebug( "FloatEdit: Adding modules." );

			$parser->getOutput()->addModules( 'ext.FloatEdit' );

		} else {

			static $loaded = false;

			// load JavaScript and CSS files only once
			if ( $loaded ) return true;

			// load extensions JavaScript
			$parser->getOutput()->addHeadItem(
				'<script type="text/javascript" src="' . $fefScriptPath
				. '/libs/floatedit.js"></script> '
			);

			// load extensions style sheet
			$parser->getOutput()->addHeadItem(
				'<link rel="stylesheet" href="' . $fefScriptPath
				. '/skins/floatedit.css"/> '
			);

			$loaded = true;

		}

		return true;
	}

}
